package com.bosssoft.itfinance.citizen.smsclient.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bosssoft.itfinance.citizen.sms.dubboservice.ISmsDubboService;
import com.bosssoft.itfinance.citizen.sms.dubboservice.req.SmsReq;
import com.bosssoft.itfinance.citizen.smsclient.risk.RiskUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author ZhiXy
 * @since 2018-04-25 14:59
 */
@Controller
public class SmsController {

    private Logger logger = LoggerFactory.getLogger(SmsController.class);

    @Reference(version = "1.0.0",
            timeout = 30000)
    ISmsDubboService smsDubboService;

    @Value("${auth.name}")
    private String authNamePro;
    @Value("${auth.code}")
    private String authCodePro;

    @RequestMapping("/sms/send/{authName}/{authCode}")
    @ResponseBody
    public void sendSms(@PathVariable String authName, @PathVariable String authCode, SmsReq smsReq) throws Exception {
        logger.info("用户：{},短信入参：{}", authName, smsReq.toString());
        if (!authNamePro.equals(authName)) {
         return;
        }
        if (!authCodePro.equals(authCode)) {
          return;
        }
        if(RiskUtil.judge(authName)){
            logger.info("用户验证通过：{},开始发送短信：{}", authName, smsReq.toString());
            //DubboFactory.getService(null,"1.0.0").sendSms(smsReq);
            smsDubboService.sendSms(smsReq);
        }else {
            logger.warn("警告，触发风控。用户：{},短信入参：{}", authName, smsReq.toString());
        }
    }

    @RequestMapping("/sms")
    @ResponseBody
    public String sms() {
        return "true";
    }

}
